import SpiderOptions from "./interfaces/spiderOptions";
const https = require("https");
const http = require("http");

// 网站有可能是http的，也有可能是https的，需要判断
export default abstract class Spider {
  // 定义成员
  options: SpiderOptions;
  httpClient: typeof http | typeof https
  constructor(options: SpiderOptions = { url: "", method: "get" }) {
    // 初始化
    this.options = options;
    this.httpClient = options.url.includes('https') ? https: http;
    this.start()
  }
  start() {
    const { url, method, headers } = this.options;
    let req = this.httpClient.request(
      url,
      {
        headers: headers,
        method: method
      },
      (res: any) => {
        let chunks: any[] = [];
        res.on("data", (chunk: any) => chunks.push(chunk));
        res.on("end", () => {
          let result = Buffer.concat(chunks).toString("utf-8");
          // 抽象方法调用
          this.oncatchHTML(result)
        });
      }
    );
    // 发送请求
    req.end();
  }
  // 抽象方法
  abstract oncatchHTML(result:any):any


}

// let result = new Spider({
//     url:"https://www.itcast.cn/news/json/f1f5ccee-1158-49a6-b7c4-f0bf40d5161a.json",
//     // url:"http://123.56.5.146/#/login",
// })
// console.log(result);
